package cn.mes.module.mes.service.basemachineinfo;

import cn.mes.module.mes.dal.dataobject.basemachineinfo.BaseMachinePeopleInfoDO;
import cn.mes.module.mes.dal.mysql.basemachineinfo.BaseMachinePeopleInfoMapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import javax.validation.Valid;

import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.mes.module.mes.controller.admin.basemachineinfo.vo.*;
import cn.mes.module.mes.dal.dataobject.basemachineinfo.BaseMachineInfoDO;
import cn.mes.module.mes.dal.dataobject.basemachineinfo.BaseDeviceDataDO;
import cn.mes.framework.common.pojo.PageResult;
import cn.mes.framework.common.pojo.PageParam;
import cn.mes.framework.common.util.object.BeanUtils;

import cn.mes.module.mes.dal.mysql.basemachineinfo.BaseMachineInfoMapper;
import cn.mes.module.mes.dal.mysql.basemachineinfo.BaseDeviceDataMapper;

import static cn.mes.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.mes.module.mes.enums.ErrorCodeConstants.*;

/**
 * 机台产线基础信息 Service 实现类
 *
 * @author 游创
 */
@Service
@Validated
public class BaseMachineInfoServiceImpl implements BaseMachineInfoService {

    @Resource
    private BaseMachineInfoMapper baseMachineInfoMapper;
    @Resource
    private BaseDeviceDataMapper baseDeviceDataMapper;
    @Resource
    private BaseMachinePeopleInfoMapper baseMachinePeopleInfoMapper;

    @Override
    public String createBaseMachineInfo(BaseMachineInfoSaveReqVO createReqVO) {
        // 插入
        BaseMachineInfoDO baseMachineInfo = BeanUtils.toBean(createReqVO, BaseMachineInfoDO.class);
        baseMachineInfoMapper.insert(baseMachineInfo);
        // 返回
        return baseMachineInfo.getId();
    }

    @Override
    public void updateBaseMachineInfo(BaseMachineInfoSaveReqVO updateReqVO) {
        // 校验存在
        validateBaseMachineInfoExists(updateReqVO.getId());
        // 更新
        BaseMachineInfoDO updateObj = BeanUtils.toBean(updateReqVO, BaseMachineInfoDO.class);
        baseMachineInfoMapper.updateById(updateObj);
    }

    @Override
    public void updateBaseMachineInfoBatch(@Valid List<BaseMachineInfoDO> baseMachineInfoDOList) {
        baseMachineInfoMapper.updateBatch(baseMachineInfoDOList);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteBaseMachineInfo(String id) {
        // 校验存在
        validateBaseMachineInfoExists(id);
        // 删除
        baseMachineInfoMapper.deleteById(id);

        // 删除子表
        deleteBaseDeviceDataByDeviceId(id);
    }

    private void validateBaseMachineInfoExists(String id) {
        if (baseMachineInfoMapper.selectById(id) == null) {
            throw exception(BASE_MACHINE_INFO_NOT_EXISTS);
        }
    }

    @Override
    public BaseMachineInfoDO getBaseMachineInfo(String id) {
        return baseMachineInfoMapper.selectById(id);
    }

    @Override
    public List<BaseMachineInfoDO> getBaseMachineInfoList() {
        QueryWrapper<BaseMachineInfoDO> baseMachineInfoDOQueryWrapper=new QueryWrapper<>();
        baseMachineInfoDOQueryWrapper.orderByAsc("machine_name");
        return baseMachineInfoMapper.selectList(baseMachineInfoDOQueryWrapper);
    }

    @Override
    public List<BaseMachineInfoDO> getBaseMachineInfoListByRegionId(String regionId){
        QueryWrapper<BaseMachineInfoDO> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("region_id",regionId);
        return baseMachineInfoMapper.selectList(queryWrapper);
    }

    @Override
    public PageResult<BaseMachineInfoDO> getBaseMachineInfoPage(BaseMachineInfoPageReqVO pageReqVO) {
        return baseMachineInfoMapper.selectPage(pageReqVO);
    }

    // ==================== 子表（设备数据） ====================

    @Override
    public PageResult<BaseDeviceDataDO> getBaseDeviceDataPage(PageParam pageReqVO, String deviceId) {
        return baseDeviceDataMapper.selectPage(pageReqVO, deviceId);
    }

    @Override
    public String createBaseDeviceData(BaseDeviceDataDO baseDeviceData) {
        baseDeviceDataMapper.insert(baseDeviceData);
        return baseDeviceData.getId();
    }

    @Override
    public boolean createBaseDeviceDataBatch(List<BaseDeviceDataDO> baseDeviceDataDOList) {
        return baseDeviceDataMapper.insertOrUpdateBatch(baseDeviceDataDOList);
    }

    @Override
    public void updateBaseDeviceData(BaseDeviceDataDO baseDeviceData) {
        // 校验存在
        validateBaseDeviceDataExists(baseDeviceData.getId());
        // 更新
        baseDeviceDataMapper.updateById(baseDeviceData);
    }

    @Override
    public void deleteBaseDeviceData(String id) {
        // 校验存在
        validateBaseDeviceDataExists(id);
        // 删除
        baseDeviceDataMapper.deleteById(id);
    }

    @Override
    public BaseDeviceDataDO getBaseDeviceData(String id) {
        return baseDeviceDataMapper.selectById(id);
    }

    private void validateBaseDeviceDataExists(String id) {
        if (baseDeviceDataMapper.selectById(id) == null) {
            throw exception(BASE_DEVICE_DATA_NOT_EXISTS);
        }
    }

    private void deleteBaseDeviceDataByDeviceId(String deviceId) {
        baseDeviceDataMapper.deleteByDeviceId(deviceId);
    }


    // ==================== 子表（机台负责人员信息） ====================

    @Override
    public PageResult<BaseMachinePeopleInfoDO> getBaseMachinePeopleInfoPage(PageParam pageReqVO, String machineId) {
        return baseMachinePeopleInfoMapper.selectPage(pageReqVO, machineId);
    }

    @Override
    public String createBaseMachinePeopleInfo(BaseMachinePeopleInfoDO baseMachinePeopleInfo) {
        String id=baseMachinePeopleInfo.getMachineId()+baseMachinePeopleInfo.getPositionCode()+baseMachinePeopleInfo.getWorkerJobNo();
        baseMachinePeopleInfo.setId(id);
        baseMachinePeopleInfoMapper.insert(baseMachinePeopleInfo);
        return baseMachinePeopleInfo.getId();
    }

    @Override
    public void updateBaseMachinePeopleInfo(BaseMachinePeopleInfoDO baseMachinePeopleInfo) {
        // 校验存在
        validateBaseMachinePeopleInfoExists(baseMachinePeopleInfo.getId());
        // 更新
        baseMachinePeopleInfoMapper.updateById(baseMachinePeopleInfo);
    }

    @Override
    public void deleteBaseMachinePeopleInfo(String id) {
        // 校验存在
        validateBaseMachinePeopleInfoExists(id);
        // 删除
        baseMachinePeopleInfoMapper.deleteById(id);
    }

    @Override
    public BaseMachinePeopleInfoDO getBaseMachinePeopleInfo(String id) {
        return baseMachinePeopleInfoMapper.selectById(id);
    }

    private void validateBaseMachinePeopleInfoExists(String id) {
        if (baseMachinePeopleInfoMapper.selectById(id) == null) {
            throw exception(BASE_MACHINE_PEOPLE_INFO_NOT_EXISTS);
        }
    }

    private void deleteBaseMachinePeopleInfoByMachineId(String machineId) {
        baseMachinePeopleInfoMapper.deleteByMachineId(machineId);
    }
}